const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    open_id: '',
    runData: [],
    phone: '',
    modalName: '',
    sessionKey: '',
    declare: '',
    prize: '',
    hdgz: '',
    shareText: '',
    title: '',
    isCard: false,
    visit_count: 0,
    like_count: 0,
    user_count: 0,
    swiperList: [],
    TabCur: 0
  },
  onLoad() {
    wx.showShareMenu({
      withShareTicket: true
    })

    const sessionKey = wx.getStorageSync('sessionKey');
    const open_id = wx.getStorageSync('user_new');
    const phone = wx.getStorageSync('phone');
    const declare = wx.getStorageSync('declare');

    if (declare) {
      this.setData({
        declare: declare,
      })
    }

    this.setData({
      open_id: open_id,
      phone: phone,
      sessionKey: sessionKey,
    })

    if (sessionKey == undefined || sessionKey == '') {
      this.setData({
        open_id: '',
      })

      wx.setStorageSync('user_new', '')
    }

    this.getRunDdata()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: this.data.shareText,
      path: '/pages/mmj/index'
    }
  },

  onPullDownRefresh: function() {
    this.getRunDdata()
    setTimeout(() => {
      wx.showToast({
        title: '刷新成功'
      })
      wx.stopPullDownRefresh()
    }, 1000)

    return true;
  },

  getRunDdata() {
    const that = this
    app.core('get', 'user/whmmj?type=' + this.data.TabCur, {}, (res) => {
      if (res.resultCode == 200) {
        that.setData({
          runData: res.data,
          prize: res.prize,
          hdgz: res.hdgz,
          title: res.title,
          visit_count: res.visit_count,
          like_count: res.like_count,
          user_count: res.user_count,
          swiperList: res.tops,
        })

        if (res.shareText != '') {
          that.setData({
            shareText: res.shareText
          })
        }

        if (res.declare != '' && that.data.declare == '') {
          that.setData({
            declare: res.declare
          })
        }
      }
    })
  },

  handleContent: function () {
    wx.previewImage({
      current: this.data.hdgz,
      urls: [
        this.data.hdgz,
      ]
    })
  },

  handlePrize: function () {
    wx.previewImage({
      current: this.data.prize,
      urls: [
        this.data.prize,
      ]
    })
  },

  getPhoneNumber (e) {
    const that = this
    if (e.detail.iv && e.detail.encryptedData) {
      app.core('post', 'user/getPhoneNumber', {encrypted_data: e.detail.encryptedData, iv: e.detail.iv, open_id: this.data.open_id}, (res) => {
        if (res.resultCode == 200) {
          if (res.data.purePhoneNumber) {
            wx.setStorageSync("phone", res.data.purePhoneNumber)
            that.setData({
              phone: res.data.purePhoneNumber
            })
          } else {
            wx.showToast({
              title: '获取手机号失败',
            })
          }
        } else {
          wx.showToast({
            title: '获取手机号失败',
          })
        }
      })
    } else {
      wx.showToast({
        title: '获取手机号失败',
      })
    }
  },

  toLogin: function () {
    wx.reLaunch({
      url: '/pages/member/index?redirect=' + encodeURIComponent('/pages/runData/index')
    })

    return false
  },

  hideModal() {
    this.setData({
      modalName: ''
    })
  },

  showModal() {
    this.setData({
      modalName: 'show'
    })
  },

  handleModal() {
    if (this.data.declare == '') {

      wx.showToast({
        title: '参赛宣言不能为空',
      })

      return
    }

    this.runData()
    this.hideModal()
  },

  handleInput(e) {
    if (e.detail.value) {
      if (e.detail.value != '' && e.detail.value.length > 50) {
        wx.showToast({
          title: '参赛宣言太长啦！',
        })

        e.detail.value = e.detail.value.substr(0, 50)
      }

      this.setData({
        declare: e.detail.value
      })
    }
  },

  handleDetail(e) {
    const id = e.target.dataset.aid
    console.log(e)
    // wx.navigateTo({
    //   url: '/pages/write/detail?id='
    // })
  },

  handleAdd() {
    wx.navigateTo({
      url: '/pages/writeform/index?title=' + this.data.title + '&category=3'
    })
  },

  preview(e) {
    wx.previewImage({
      urls: e,
      current: this.data.swiperList
    });
  },

  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id-1)*60
    })

    this.getRunDdata()
  },

  advJump (e) {

    let swiperList = this.data.swiperList

    let val = swiperList[e.currentTarget.dataset.index]
    if (val) {
      if ( val['app_id'] != undefined && val['app_id'] != null && val['app_id'] != '') {
        wx.navigateToMiniProgram({
          appId: val['app_id'],
          path: val['redirect_url'],
          extraData: {},
          envVersion: 'release',
          success(res) {
            console.log(res)
            // 打开成功
          },
          fail(e) {
            console.log(e)
          }
        })
      } else if ( val['redirect_url'] != undefined && val['redirect_url'] != null && val['redirect_url'] != '') {

        let url = val['redirect_url']

        if (url.indexOf("/pages") == 0) {
          wx.navigateTo({
            url: url,
          })
        } else if (url.indexOf("http") == 0) {
          wx.navigateTo({
            url: '/pages/webView/index?url='+ decodeURIComponent(url),
          })
        }
      }

    }
  },

  advJump (e) {

    let swiperList = this.data.swiperList

    let val = swiperList[e.currentTarget.dataset.index]
    if (val) {
      if ( val['app_id'] != undefined && val['app_id'] != null && val['app_id'] != '') {
        wx.navigateToMiniProgram({
          appId: val['app_id'],
          path: val['redirect_url'],
          extraData: {},
          envVersion: 'release',
          success(res) {
            console.log(res)
            // 打开成功
          },
          fail(e) {
            console.log(e)
          }
        })
      } else if ( val['redirect_url'] != undefined && val['redirect_url'] != null && val['redirect_url'] != '') {

        let url = val['redirect_url']

        if (url.indexOf("/pages") == 0) {
          wx.navigateTo({
            url: url,
          })
        } else if (url.indexOf("http") == 0) {
          wx.navigateTo({
            url: '/pages/webView/index?url='+ decodeURIComponent(url),
          })
        }
      }

    }
  }

})
